package com.yunsc.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.media.dao.YunscUploadGroupDao;
import com.yunsc.media.dto.FileGroupDTO;
import com.yunsc.media.entity.YunscUploadGroupEntity;
import com.yunsc.media.service.YunscUploadGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



@Service("yunscUploadGroupService")
public class YunscUploadGroupServiceImpl extends ServiceImpl<YunscUploadGroupDao, YunscUploadGroupEntity> implements YunscUploadGroupService {


    @Override
    public List<YunscUploadGroupEntity> queryFileGroupList(Integer storeId) {
        LambdaQueryWrapper<YunscUploadGroupEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscUploadGroupEntity::getStoreId, storeId);
        lqw.eq(YunscUploadGroupEntity::getIsDelete, 0);
        return this.list(lqw);
    }

    @Override
    @Transactional
    public void addFileGroup(FileGroupDTO fileGroupDTO, Integer storeId) {
        YunscUploadGroupEntity groupEntity = new YunscUploadGroupEntity();
        BeanUtils.copyProperties(fileGroupDTO.getForm(), groupEntity);
        groupEntity.setStoreId(storeId);
        this.save(groupEntity);
    }

    @Override
    @Transactional
    public void editFileGroup(FileGroupDTO fileGroupDTO) {
        Integer groupId = fileGroupDTO.getGroupId();
        YunscUploadGroupEntity groupEntity = this.getById(groupId);
        BeanUtils.copyProperties(fileGroupDTO.getForm(), groupEntity);
        groupEntity.setGroupId(groupId);
        this.updateById(groupEntity);
    }

    @Override
    @Transactional
    public void removeFileGroup(FileGroupDTO fileGroupDTO) {
        UpdateWrapper<YunscUploadGroupEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("group_id", fileGroupDTO.getGroupId());
        updateWrapper.set("is_delete", 1);
        this.update(updateWrapper);
    }
}